package com.kingre.et.dao.jpa;

import java.util.List;

import com.kingre.et.dao.IConsultDAO;
import com.kingre.et.model.Consult;
import com.kingre.et.model.School;
import com.kingre.et.model.User;

public class ConsultDAOJPA extends GenericDAOJPA<Consult, Integer> implements IConsultDAO {

    @SuppressWarnings("unchecked")
    public List<Consult> findAll(String userName, Integer consultStauts){
        if(userName == null)
            userName = "";
        String hql;
        if(consultStauts == null || consultStauts == -1){
            hql = "SELECT DISTINCT p FROM Consult p, IN(p.user) o  WHERE o.name like ? ORDER BY p.actionTime";
            return getJpaTemplate().find(hql, "%" + userName + "%");
        }
        else{
            hql = "SELECT DISTINCT p FROM Consult p, IN(p.user) o  WHERE o.name like ? AND p.statusCode = ? ORDER BY p.actionTime";
            return getJpaTemplate().find(hql, "%" + userName + "%", consultStauts);            
        }
        
    }

    @SuppressWarnings("unchecked")
    public List<Consult> findByExpert(User expert, String userName, Integer consultStauts) {
        if(userName == null)
            userName = "";
        String hql;
        if(consultStauts == null || consultStauts == -1){
            hql = "SELECT DISTINCT p FROM Consult p, IN(p.user) o  WHERE p.export = ? AND o.name like ? ORDER BY p.actionTime";
            return getJpaTemplate().find(hql, expert, "%" + userName + "%");
        }
        else{
            hql = "SELECT DISTINCT p FROM Consult p, IN(p.user) o  WHERE p.export = ? AND o.name like ? AND p.statusCode = ? ORDER BY p.actionTime";
            return getJpaTemplate().find(hql, expert, "%" + userName + "%", consultStauts);            
        }
    }

    @SuppressWarnings("unchecked")
    public List<Consult> findByTeacher(School school, String userName, Integer consultStauts) {
        if(userName == null)
            userName = "";
        String hql;
        if(consultStauts == null || consultStauts == -1){
            hql = "SELECT DISTINCT p FROM Consult p, IN(p.user) o  WHERE p.school = ? AND o.name like ? ORDER BY p.actionTime";
            return getJpaTemplate().find(hql, school, "%" + userName + "%");
        }
        else{
            hql = "SELECT DISTINCT p FROM Consult p, IN(p.user) o  WHERE p.school = ? AND o.name like ? AND p.statusCode = ? ORDER BY p.actionTime";
            return getJpaTemplate().find(hql, school, "%" + userName + "%", consultStauts);            
        }
    }
}
